﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.admin.service;
using bzxxxt.bll;
using bzxxxt.dal;
using bzxxxt.model;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// bg_grave 的摘要说明
    /// </summary>
    public class bg_grave : admin.ashx.rm_base
    {

        GraveBLL graveBLL = new GraveBLL();

        public override void add(HttpContext context)
        {
            string paradisecode = context.Request.Params["ui_grave_paradise_code"] ?? "";
            string hourse = context.Request.Params["ui_grave_hourseNum"] ?? "0";
            if (string.IsNullOrEmpty(hourse)) hourse = "1";

            string floor = context.Request.Params["ui_grave_floorNum"] ?? "0";
            if (string.IsNullOrEmpty(floor)) floor = "0";

            string section = context.Request.Params["ui_grave_sectionNum"] ?? "0";
            if (string.IsNullOrEmpty(section)) section = "0";

            string line = context.Request.Params["ui_grave_lineNum"] ?? "0";
            if (string.IsNullOrEmpty(line)) line = "0";

            string row = context.Request.Params["ui_grave_rowNum"] ?? "0";
            if (string.IsNullOrEmpty(row)) row = "0";

            string col = context.Request.Params["ui_grave_colNum"] ?? "0";
            if (string.IsNullOrEmpty(col)) col = "0";

            string type = context.Request.Params["ui_grave_type"] ?? "1";
            if (string.IsNullOrEmpty(type)) type = "1";

            string price = context.Request.Params["ui_grave_price"] ?? "0";
            if (string.IsNullOrEmpty(price)) price = "0";

            string qty = context.Request.Params["ui_grave_qty"] ?? "0";
            if (string.IsNullOrEmpty(qty)) qty = "0";

            string description = context.Request.Params["ui_grave_description"] ?? "";
            
            int hourseNum = int.Parse(hourse);
            int floorNum = int.Parse(floor);
            int sectionNum = int.Parse(section);
            int lineNum = int.Parse(line);
            int rowNum = int.Parse(row);
            int colNum = int.Parse(col);

            char c = Convert.ToChar('A' + int.Parse(type) - 1);
            tbGrave grave = new tbGrave
            {
                location = string.Format("{0:000}{1:0}0{2:00}{3:00}{4:00000}{5:0}", paradisecode,hourseNum, sectionNum, lineNum, colNum, c),
                name = string.Format("{0:00}区{1:00}排{2:00000}号", sectionNum, lineNum, colNum),
                hourseNum = string.Format("{0:0}", hourseNum),
                floorNum = string.Format("{0:0}", floorNum),
                sectionNum = string.Format("{0:00}", sectionNum),
                lineNum = string.Format("{0:00}", lineNum),
                layerNum = string.Format("{0:00}", rowNum),
                columnNum = string.Format("{0:0000}", colNum),
                qty = int.Parse(qty),
                graveType = int.Parse(type),
                price = float.Parse(price),
                description = description
            };

            context.Response.Write(graveBLL.Add(grave, paradisecode));
        }

        public override void edit(HttpContext context)
        {
            string id = context.Request.Params["ui_grave_id"] ?? "0";
            string paradisecode = context.Request.Params["ui_grave_paradise_code"] ?? "";
            string hourse = context.Request.Params["ui_grave_hourseNum"] ?? "0";
            if (string.IsNullOrEmpty(hourse)) hourse = "1";

            string floor = context.Request.Params["ui_grave_floorNum"] ?? "0";
            if (string.IsNullOrEmpty(floor)) floor = "0";

            string section = context.Request.Params["ui_grave_sectionNum"] ?? "0";
            if (string.IsNullOrEmpty(section)) section = "0";

            string line = context.Request.Params["ui_grave_lineNum"] ?? "0";
            if (string.IsNullOrEmpty(line)) line = "0";

            string row = context.Request.Params["ui_grave_rowNum"] ?? "0";
            if (string.IsNullOrEmpty(row)) row = "0";

            string col = context.Request.Params["ui_grave_colNum"] ?? "0";
            if (string.IsNullOrEmpty(col)) col = "0";

            string type = context.Request.Params["ui_grave_type"] ?? "1";
            if (string.IsNullOrEmpty(type)) type = "1";

            string price = context.Request.Params["ui_grave_price"] ?? "0";
            if (string.IsNullOrEmpty(price)) price = "0";

            string qty = context.Request.Params["ui_grave_qty"] ?? "0";
            if (string.IsNullOrEmpty(qty)) qty = "0";

            string description = context.Request.Params["ui_grave_description"] ?? "";

            int hourseNum = int.Parse(hourse);
            int floorNum = int.Parse(floor);
            int sectionNum = int.Parse(section);
            int lineNum = int.Parse(line);
            int rowNum = int.Parse(row);
            int colNum = int.Parse(col);

            char c = Convert.ToChar('A' + int.Parse(type) - 1);
            tbGrave grave = new tbGrave
            {
                id = int.Parse(id),
                location = string.Format("{0:000}{1:0}0{2:00}{3:00}{4:00000}{5:0}", paradisecode, hourseNum, sectionNum, lineNum, colNum, c),
                //location = string.Format("{0:000}{1:0}0{2:00}{3:00}{4:00000}", paradisecode, hourseNum, sectionNum, lineNum, colNum),
                name = string.Format("{0:00}区{1:00}排{2:00000}号", sectionNum, lineNum, colNum),
                hourseNum = string.Format("{0:0}", hourseNum),
                floorNum = string.Format("{0:0}", floorNum),
                sectionNum = string.Format("{0:00}", sectionNum),
                lineNum = string.Format("{0:00}", lineNum),
                layerNum = string.Format("{0:00}", rowNum),
                columnNum = string.Format("{0:0000}", colNum),
                qty = int.Parse(qty),
                graveType = int.Parse(type),
                price = float.Parse(price),
                description = description
            };
            
            context.Response.Write(graveBLL.Edit(grave, paradisecode));
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["id"];
            context.Response.Write(graveBLL.Delete(ids));
        }

        public override void getall(HttpContext context)
        {
            string where = context.Request.Params["where"] ?? "1=1";
            int pageIndex = CommUtil.GetPageIndex(context);
            int pageSize = CommUtil.GetPageSize(context);
            string mySort = CommUtil.GetSort(context) + " " + CommUtil.GetOrder(context);

            context.Response.Write(graveBLL.GetListByPage(pageSize, pageIndex, mySort, where));
        }

        public override void search(HttpContext context)
        {
            string where = context.Request.Params["where"] ?? "1=1";
            int pageIndex = CommUtil.GetPageIndex(context);
            int pageSize = CommUtil.GetPageSize(context);
            string mySort = CommUtil.GetSort(context) + " " + CommUtil.GetOrder(context);

            context.Response.Write(graveBLL.GetListByPage(pageSize, pageIndex, mySort, where));
        }
        public override void other(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "import": {
                    string op = context.Request.Params["op"];
                    if (op == "upload")
                    {
                        UserEntity user = UserService.GetUser(context);
                        //文件名  
                        HttpPostedFile f1 = context.Request.Files["ui_grave_import_file"];

                        string fileExt = System.IO.Path.GetExtension(f1.FileName);
                        if (!(fileExt == ".xls" || fileExt == ".xlsx"))
                        {
                            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "只支持扩展名为.xls或.xlsx文件！" }));
                            return;
                        }
                        string fileName = user.userName + "_grave_" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
                        f1.SaveAs(context.Server.MapPath("~/admin/upload/") + fileName);
                        context.Response.Write(JsonUtil.ToJson(new { success = true, msg = fileName }));
                    }
                    else if (op == "import") {
                        string file = context.Request.Params["ui_grave_import_filename"];

                        if (string.IsNullOrEmpty(file))
                        {
                            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "文件未上传！" }));
                        }
                        else {
                            file = context.Server.MapPath("~/admin/upload/") + file;
                            try
                            {
                                UserEntity user = UserService.GetUser(context);
                                List<ImportModel> list = graveBLL.ImportExceltoDb(user,file, 0, 1);
                                graveBLL.Import(list);
                                context.Response.Write(JsonUtil.ToJson(new { success = true, msg = "导入成功！" }));
                            }
                            catch (Exception ex) {

                                context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "导入失败: "+ex.Message }));
                            }
                        }
                    }
                    break;
                }
                case "clear": {
                    string ids = context.Request.Params["ui_dead_id"];
                    string graveId = context.Request.Params["ui_grave_id"];
                    string remark = context.Request.Params["ui_grave_remark"];
                    context.Response.Write(graveBLL.removeDeadFromGrave(int.Parse(graveId), ids, remark));
                    break;
                }
                case "move":
                    {
                        string graveId = context.Request.Params["ui_grave_id"];
                        string deadId = context.Request.Params["ui_dead_id"];
                        
                        string paradiseCode = context.Request.Params["ui_grave_paradise_code"];
                        string hourse = context.Request.Params["ui_grave_hourseNum"];
                        string floor = context.Request.Params["ui_grave_floorNum"];
                        string section = context.Request.Params["ui_grave_sectionNum"];
                        string line = context.Request.Params["ui_grave_lineNum"];
                        string layer = context.Request.Params["ui_grave_rowNum"];
                        string column = context.Request.Params["ui_grave_colNum"];
                        string remark = context.Request.Params["ui_grave_move_remark"];
                        int oldGraveId = int.Parse(graveId);
                        int hourseNum = int.Parse(hourse);
                        int floorNum = int.Parse(floor);
                        int sectionNum = int.Parse(section);
                        int lineNum = int.Parse(line);
                        int layerNum = int.Parse(layer);
                        int colNum = int.Parse(column);
                        section = string.Format("{0:00}", sectionNum);
                        tbGrave grave = new tbGrave
                        {
                            location = string.Format("{0}{1:0}{2:0}{3:00}{4:00}{5:00}{6:000}", paradiseCode, hourseNum, floorNum, sectionNum, lineNum, layerNum, colNum),
                            name = string.Format("{1}栋{2}楼{3}区{4}排{5}层{6}号", paradiseCode, hourseNum, floorNum, sectionNum, lineNum, layerNum, colNum),
                            hourseNum = hourse,
                            floorNum = floor,
                            sectionNum = section,
                            lineNum = string.Format("{0:00}",lineNum),
                            layerNum =  string.Format("{0:00}",layerNum),
                            columnNum =  string.Format("{0:00}",colNum)                           
                        };
                        if (string.IsNullOrEmpty(deadId))
                        {
                            context.Response.Write(graveBLL.Move(oldGraveId, grave,remark));
                        }
                        else {
                            context.Response.Write(graveBLL.Move(oldGraveId, grave,int.Parse( deadId),remark));
                        }
                        break;
                    }
                case "store":
                    {
                        string graveId = context.Request.Params["ui_grave_id"];
                        int gid = int.Parse(graveId);

                        string deadId = context.Request.Params["ui_grave_dead_id"];
                        string deadName = context.Request.Params["ui_grave_dead_name"];
                        string deadCardNo = context.Request.Params["ui_grave_dead_cardno"];
                        string deadGender = context.Request.Params["ui_grave_dead_gender"];
                        string deadNation = context.Request.Params["ui_grave_dead_nation"];
                        string deadArea = context.Request.Params["ui_grave_dead_areaId"];
                        string relation = context.Request.Params["ui_grave_relative_relation"];
                        string storeType = context.Request.Params["ui_grave_store_type"];
                        string addr = context.Request.Params["ui_grave_dead_addr"];
                        if (string.IsNullOrEmpty(storeType))
                        {
                            storeType = "遗灰";
                        }
                        string fireTime = context.Request.Params["ui_grave_dead_firetime"];
                        tbDead dead = new tbDead();
                        if (!string.IsNullOrEmpty(deadId))
                        {
                            dead.id = int.Parse(deadId);
                        }
                        if (!string.IsNullOrEmpty(fireTime)) {
                            dead.actFiretime = DateTime.Parse(fireTime);
                        }
                        dead.name = deadName;
                        dead.gender = deadGender;
                        dead.cardno = deadCardNo;
                        dead.nationid = deadNation;
                        dead.relationid = relation;
                        dead.storetype = storeType;
                        dead.areacode = deadArea;
                        dead.homeAddress = addr;
                        dead.birthAddress = addr;

                        string relativeId = context.Request.Params["ui_grave_relative_id"];
                        string rName = context.Request.Params["ui_grave_relative_name"];
                        string rTel = context.Request.Params["ui_grave_relative_tel"];
                        string rArea = context.Request.Params["ui_grave_relative_areaId"];
                        string rCardno = context.Request.Params["ui_grave_relative_cardno"];


                        tbRelative relative = new tbRelative();
                        if (!string.IsNullOrEmpty(relativeId))
                        {
                            relative.id = int.Parse(relativeId);                                            
                        }
                        relative.name = rName;
                        relative.tel = rTel;
                        relative.areacode = rArea;
                        relative.cardno = rCardno;
                        
                                                
                        context.Response.Write(graveBLL.Store(dead,relative,gid));
                        break;
                    }

                case "sellgrave":
                    {
                        string relativeId = context.Request.Params["ui_grave_relative_id"];
                        if (string.IsNullOrEmpty(relativeId))
                        {
                            context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "请选择联系人!" }));
                            return;
                        }
                        string graveId = context.Request.Params["ui_grave_id"];
                        string paid = context.Request.Params["ui_grave_paid"];
                        int rid = int.Parse(relativeId);
                        int gid = int.Parse(graveId);
                        context.Response.Write(graveBLL.Sale(rid, gid, float.Parse(paid)));
                        break;
                    }               
            }
        }
    }
}